﻿/****************************************************************
 * Copyright (c) 2010-2013 Besture All Rights Reserved.
 * 
 * File Name:		ContentDal 
 * 
 * Created:		2013-7-22 16:42:38
 * Original Author:	nitao 
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/

using System;
using System.Data;
using System.Data.Common;
using System.Text;
using Besture.Common;
using Besture.Model;

namespace Besture.Dal
{
    public class ContentDal : BaseDataAccess
    {
        /// <summary>
        /// 根据id获取单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ContentModel GetModel(int id)
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBContent");
                selectSql.Append(" WHERE ");
                selectSql.Append(" ID=@ID ");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.Int32, id);
                DataTable dt = base.GetDataSet(ldcDBCommand).Tables[0];
                if (dt.Rows.Count > 0)
                    return ModelConvertHelper<ContentModel>.ConvertToModelList(dt)[0];
                else return null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataTable()
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBContent");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                DataTable dt = base.GetDataSet(ldcDBCommand).Tables[0];
                return dt;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(ContentModel model)
        {
            var identityId = -1;
            try
            {
                StringBuilder insertSql = new StringBuilder();
                insertSql.Append("INSERT INTO TBContent(");
                insertSql.Append("EntryKey,");
                insertSql.Append("ContentDetail,");
                insertSql.Append("CreateTime");

                insertSql.Append(") ");
                insertSql.Append(" VALUES(");
                insertSql.Append("@EntryKey,");
                insertSql.Append("@ContentDetail,");
                insertSql.Append("@CreateTime");

                insertSql.Append(")");
                insertSql.Append(" SELECT @@IDENTITY");
                DbCommand ldcDBCommand = base.GetCommand(insertSql.ToString());
                base.SetParameter(ldcDBCommand, "@EntryKey", DbType.String, model.EntryKey);
                base.SetParameter(ldcDBCommand, "@ContentDetail", DbType.String, model.ContentDetail);
                base.SetParameter(ldcDBCommand, "@CreateTime", DbType.DateTime, model.CreateTime);

                identityId = base.ExecuteScalarInsert(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return identityId;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(ContentModel model)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("UPDATE TBContent");
                updateSql.Append(" SET");
                updateSql.Append(" EntryKey = @EntryKey,");
                updateSql.Append(" ContentDetail = @ContentDetail,");
                updateSql.Append(" CreateTime = @CreateTime ");

                updateSql.Append(" WHERE ");
                updateSql.Append(" ID = @ID ");

                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@EntryKey", DbType.String, model.EntryKey);
                base.SetParameter(ldcDBCommand, "@ContentDetail", DbType.String, model.ContentDetail);
                base.SetParameter(ldcDBCommand, "@CreateTime", DbType.DateTime, model.CreateTime);

                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("DELETE FROM TBContent");
                updateSql.Append(" WHERE ");
                updateSql.Append(" ID=@ID ");
                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.Int32, id);
                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public int DeleteList(string ids)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("exec('DELETE FROM TBContent");
                updateSql.Append(" WHERE ");
                updateSql.Append(" ID in ('+@ID+') ')");
                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.Int32, ids);
                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }


        public void SetContent(string entryKey, string content, string EntryTitle)
        {
            try
            {
                //StringBuilder selectSql = new StringBuilder();
                //selectSql.Append("SELECT * FROM TBContent");
                //selectSql.Append(" WHERE Entry=@Entry ");
                //DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                //base.SetParameter(ldcDBCommand, "@Entry", DbType.String, entryKey);
                //DataTable dt = base.GetDataSet(ldcDBCommand).Tables[0];

                //if (dt.Rows.Count == 0)
                //{
                //    string sql = "INSERT INTO TBContent (Entry,EntryTitle,ContentDetail) VALUES(@Entry,@EntryTitle,@ContentDetail)";
                //    dbCommand = base.GetCommand(sql.ToString());
                //    base.SetParameter(dbCommand, "@Entry", DbType.String, entryKey);
                //    base.SetParameter(dbCommand, "@EntryTitle", DbType.String, entryKey);
                //    base.SetParameter(dbCommand, "@ContentDetail", DbType.String, content);
                //}
                //else
                //{
                string sql = "UPDATE TBContent SET ContentDetail = @ContentDetail WHERE EntryKey = @EntryKey";
                    DbCommand  dbCommand = base.GetCommand(sql.ToString());
                    base.SetParameter(dbCommand, "@ContentDetail", DbType.String, content);
                    base.SetParameter(dbCommand, "@EntryKey", DbType.String, entryKey);
                //};
                base.ExecuteNonQuery(dbCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public ContentModel GetContent(string entry)
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBContent");
                selectSql.Append(" WHERE EntryKey=@EntryKey ");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                base.SetParameter(ldcDBCommand, "@EntryKey", DbType.String, entry);
                DataTable dt = base.GetDataSet(ldcDBCommand).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    return ModelConvertHelper<ContentModel>.ConvertToModelList(dt)[0];
                }
                else
                {
                    return new ContentModel();
                };
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
